<div class="modal-dialog" role="document">
    <div class="modal-content">
        <div class="modal-header">
            <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
            <h4 class="modal-title" id="exampleModalLabel">添加管理员</h4>
        </div>
        <form style="margin:4%;" id="addRoleForm">
            <div class="form-group">
                <label for="recipient-name" class="control-label">街道：</label>
                {$orgInfo.name}
            </div>
            <div class="form-group">
                <label for="recipient-name" class="control-label">用户：</label>
                <select id="userList" name="userList" class="form-control" style="width: 150px;">
                    <option value="">请选择</option>
                    <volist name="userList" id="user">
                        <option value="{$user.id}">{$user.real_name}</option>
                    </volist>

                </select>
            </div>
            <div id="adminListDiv" class="form-group">
                <volist name="adminList['admins']" id="admin">
                    <button class="btn btn-primary" style="margin-right:5px;margin-top:10px;" type="button">
                        <span thisId="{$admin.id}">&times;</span>  {$admin.real_name} 
                    </button>

                </volist>

            </div>
        </form>

        <div class="modal-footer">
            <button type="button" data-dismiss="modal" class="btn btn-default">取消</button>
            <button type="button" id="adminSave" class="btn btn-primary">保存</button>
        </div>
    </div>
</div>
<script>
    
    var adminList = [];
    var orgId = "{$orgId}";

    function init(){
        $("#adminListDiv").find("span").each(function(k,item){
            adminList.push(parseInt($(item).attr("thisId")));
        })
        //console.log(adminList);
    }
    
    $(document).ready(function () {
        init();
        $("#userList").select2({
            placeholder: "选择管理员"
        });
        
        $("#userList").change(function(){
            var checkUserId = parseInt($("#userList").select2("val"));
            //console.log(checkUserId);
            if(!common.inArray(checkUserId,adminList)){
                adminList.push(checkUserId);
                addAdminList(checkUserId,$("#userList").select2('data')[0].text);
                $("#userList").select2("val","");
            }
        });
        
        $("#adminListDiv").on('click','span',function(){
            //console.log($(this).attr("thisId"));
            adminList.remove(parseInt($(this).attr("thisId")));
            //console.log(adminList);
            $(this).parent("button").remove();
        })
        
        $("#adminSave").click(function(){
            if(common.isEmptyArray(adminList)){
                common.alert("提示","请选择管理员!");
                return false;
            }
            common.ajax("{:U('Admin/Org/doSetOrgAdmin')}",{adminList:adminList,orgId:orgId},function(data){
                if(data){
                    common.alert("提示","管理员设置成功!",function(){
                        common.reload();
                    })
                }
            })
        });
    });
        
        
    function addAdminList(id,name){
        $("#adminListDiv").append('<button class="btn btn-primary" style="margin-right:5px;margin-top:10px;" type="button">'+
                        '<span thisId="'+id+'">&times;</span> '+name+
                    '</button>');
    }
    
</script>